import { axios } from '@/utils/request'
import axiosNpm from "axios";
const CancelToken = axiosNpm.CancelToken;
// 上传附件在js是

// 小文件上传附件即通用文件上传
export function uploadAttach (options) {
  let formData = new FormData()
  if (options.data) {
    Object.keys(options.data).forEach(key => {
      formData.append(key, options.data[key])
    })
  }
  formData.append(options.filename, options.file, options.file.name)

  return axios({
    url: '/tp/attach/upload',
    method: 'post',
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    timeout: 30 * 60 * 1000,
    onUploadProgress: progressEvent => {
      const percentCompleted = Math.floor((progressEvent.loaded * 100) / progressEvent.total)
      options.onProgress({ percent: percentCompleted })
    },
    data: formData
  })
}
// 合并附件

export function mergerFile (params) {
  return axios({
    url: '/com.haday.hte.attach/attachInfo/mergerFile',
    method: 'post',
    timeout: 24 * 60 * 60 * 1000,
    data: params,

  })
}
// 下载附件
export function downFile (params,options) {
  return axios({
    url: '/tp/attach/download',
    method: 'get',
    responseType: 'arraybuffer',
    params: params,
    onDownloadProgress: progressEvent => {
      const percent = Math.floor((progressEvent.loaded * 100) / progressEvent.total)
        // 保存每个分片的上传进度
      options.handleProgress(params.offset,progressEvent.loaded)
    },
    cancelToken: new CancelToken(c => {
      // 这个参数 c 就是CancelToken构造函数里面自带的取消请求的函数，这里把该函数当参数用
      // 订阅每个分片的取消事件
      options.cancelHandleList.push(c);
    })
   
    
  })
}

// 下载附件
export function downFileZip (params,options) {
  return axios({
    url: '/com.haday.hte.attach/attachInfo/download',
    method: 'get',
    responseType: 'blob',
    timeout: 0,
    params: params,
    onDownloadProgress: progressEvent => {
      const percent = Math.floor((progressEvent.loaded * 100) / progressEvent.total)
        // 保存每个分片的上传进度
        
      
      options.handleProgress(params.offset,progressEvent.loaded)
    },
    cancelToken: new CancelToken(c => {
      // 这个参数 c 就是CancelToken构造函数里面自带的取消请求的函数，这里把该函数当参数用
      // 订阅每个分片的取消事件
      options.cancelHandleList.push(c);
    })
  })
}

// 分块下载附件
export function downloadChunks (params,options) {
  return axios({
    url: '/com.haday.hte.attach/attachInfo/downloadChunks',
    method: 'get',
    responseType: 'arraybuffer',
    timeout: 24 * 60 * 60 * 1000,
    params: params,
    onDownloadProgress: progressEvent => {
      const percent = Math.floor((progressEvent.loaded * 100) / progressEvent.total)
        // 保存每个分片的上传进度
        
      
      options.handleProgress(params.offset,progressEvent.loaded)
    },
    cancelToken: new CancelToken(c => {
      // 这个参数 c 就是CancelToken构造函数里面自带的取消请求的函数，这里把该函数当参数用
      // 订阅每个分片的取消事件
      options.cancelHandleList.push(c);
    })

  })
}

/**
 * 下载附件
 * @param url 文件路径
 * @param fileName 文件名
 * @param parameter
 * @returns {*}
 */
export function downloadAttach(fileName, parameter) {
  return downFile( parameter).then((data) => {
    if (!data || data.size === 0) {
      return
    }
    if (typeof window.navigator.msSaveBlob !== 'undefined') {
      window.navigator.msSaveBlob(new Blob([data]), fileName)
    } else {
      let url = window.URL.createObjectURL(new Blob([data]))
      let link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      link.setAttribute('download', fileName)
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link) //下载完成移除元素
      window.URL.revokeObjectURL(url) //释放掉blob对象
    }
  })
}
// 绑定业务主键
export function bindBusKey (params) {
  return axios({
    url: '/tp/attach/delete/bindBusKey',
    method: 'post',
    data: params
  })
}

// 删除附件
export function deleteAttach (params) {
  return axios({
    url: '/tp/attach/delete',
    method: 'post',
    data: params
  })
}

// 获取附件
export function getAttaches (params) {
  return axios({
    url: '/com.haday.hte.attach/attachInfo/getAttaches',
    method: 'get',
    params: params
  })

}

export function deleteChunks (params) {
  return axios({
    url: '/com.haday.hte.attach/attachInfo/deleteChunks',
    method: 'post',
    data: params
  })
}

